<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" inline v-show="showSearch" label-width="68px">
      <el-form-item label="设备编号" prop="equipmentId">
        <el-input
          v-model="queryParams.equipmentId"
          placeholder="请输入设备编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="设备名称" prop="equipmentName">
        <el-input
          v-model="queryParams.equipmentName"
          placeholder="请输入设备名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="计划编号" prop="planId">
        <el-input
          v-model="queryParams.planId"
          placeholder="请输入计划编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="BOM单编号" prop="bomId">
        <el-input
          v-model="queryParams.bomId"
          placeholder="请输入BOM单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderId">
        <el-input
          v-model="queryParams.orderId"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="物料" prop="materialId">
        <el-input
          v-model="queryParams.materialId"
          placeholder="请输入物料"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺" prop="processId">
        <el-input
          v-model="queryParams.processId"
          placeholder="请输入工艺"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产线" prop="productLineId">
        <el-input
          v-model="queryParams.productLineId"
          placeholder="请输入生产线"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="工艺名称" prop="processName">
        <el-input
          v-model="queryParams.processName"
          placeholder="请输入工艺名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="生产线名称" prop="productLineName">
        <el-input
          v-model="queryParams.productLineName"
          placeholder="请输入生产线名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['carbonReport:equipmentPowerReport:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['carbonReport:equipmentPowerReport:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['carbonReport:equipmentPowerReport:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['carbonReport:equipmentPowerReport:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="equipmentPowerReportList" style="width: 100%">
      <el-table-column type="selection" width="55" />
      <el-table-column label="设备编号" prop="equipmentId" />
      <el-table-column label="设备名称" prop="equipmentName" />
      <el-table-column label="计划编号" prop="planId" />
      <el-table-column label="BOM单编号" prop="bomId" />
      <el-table-column label="订单编号" prop="orderId" />
      <el-table-column label="物料" prop="materialId" />
      <el-table-column label="工艺" prop="processId" />
      <el-table-column label="生产线" prop="productLineId" />
      <el-table-column label="工艺名称" prop="processName" />
      <el-table-column label="生产线名称" prop="productLineName" />
      <el-table-column label="操作" width="180">
        <template #default="{ row }">
          <el-button type="text" size="small" @click="handleUpdate(row)">编辑</el-button>
          <el-button type="text" size="small" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改VIEW对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="equipmentPowerReportRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="设备编号" prop="equipmentId">
          <el-input v-model="form.equipmentId" placeholder="请输入设备编号" />
        </el-form-item>
        <el-form-item label="设备名称" prop="equipmentName">
          <el-input v-model="form.equipmentName" placeholder="请输入设备名称" />
        </el-form-item>
        <el-form-item label="计划编号" prop="planId">
          <el-input v-model="form.planId" placeholder="请输入计划编号" />
        </el-form-item>
        <el-form-item label="BOM单编号" prop="bomId">
          <el-input v-model="form.bomId" placeholder="请输入BOM单编号" />
        </el-form-item>
        <el-form-item label="订单编号" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单编号" />
        </el-form-item>
        <el-form-item label="物料" prop="materialId">
          <el-input v-model="form.materialId" placeholder="请输入物料" />
        </el-form-item>
        <el-form-item label="工艺" prop="processId">
          <el-input v-model="form.processId" placeholder="请输入工艺" />
        </el-form-item>
        <el-form-item label="生产线" prop="productLineId">
          <el-input v-model="form.productLineId" placeholder="请输入生产线" />
        </el-form-item>
        <el-form-item label="工艺名称" prop="processName">
          <el-input v-model="form.processName" placeholder="请输入工艺名称" />
        </el-form-item>
        <el-form-item label="生产线名称" prop="productLineName">
          <el-input v-model="form.productLineName" placeholder="请输入生产线名称" />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">提交</el-button>
          <el-button @click="cancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessageBox } from 'element-plus'
import {
  listEquipmentPowerReport,
  getEquipmentPowerReport,
  delEquipmentPowerReport,
  addEquipmentPowerReport,
  updateEquipmentPowerReport,
  exportEquipmentPowerReport
} from '@/api/carbonReport/equipmentPowerReport'

const equipmentPowerReportList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const queryParams = reactive({
  pageNum: 1,
  pageSize: 10,
  equipmentId: null,
  equipmentName: null,
  planId: null,
  bomId: null,
  orderId: null,
  materialId: null,
  processId: null,
  productLineId: null,
  processName: null,
  productLineName: null
});

const form = reactive({
  equipmentId: null,
  equipmentName: null,
  planId: null,
  bomId: null,
  orderId: null,
  materialId: null,
  processId: null,
  productLineId: null,
  processName: null,
  productLineName: null
});

// 获取数据
const getList = async () => {
  try {
    loading.value = true;
    const response = await listEquipmentPowerReport(queryParams);
    equipmentPowerReportList.value = response.rows;
    total.value = response.total;
  } finally {
    loading.value = false;
  }
};

// 查询处理
const handleQuery = () => {
  queryParams.pageNum = 1;
  getList();
};

// 重置查询
const resetQuery = () => {
  Object.keys(queryParams).forEach(key => {
    if (key !== 'pageNum' && key !== 'pageSize') {
      queryParams[key] = null;
    }
  });
  handleQuery();
};

// 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map(item => item.equipmentId);
  single.value = selection.length !== 1;
  multiple.value = !selection.length;
};

// 新增操作
const handleAdd = () => {
  resetForm();
  open.value = true;
  title.value = '添加VIEW';
};

// 修改操作
const handleUpdate = async (row) => {
  resetForm();
  const _equipmentId = row.equipmentId || ids.value;
  const response = await getEquipmentPowerReport(_equipmentId);
  Object.assign(form, response.data);
  open.value = true;
  title.value = '修改VIEW';
};

// 提交表单
const submitForm = async () => {
  const valid = await proxy.$refs['equipmentPowerReportRef'].validate();
  if (valid) {
    if (form.equipmentId != null) {
      await updateEquipmentPowerReport(form);
      ElMessageBox.alert('修改成功', '提示');
    } else {
      await addEquipmentPowerReport(form);
      ElMessageBox.alert('新增成功', '提示');
    }
    open.value = false;
    getList();
  }
};

// 删除操作
const handleDelete = async (row) => {
  const _equipmentIds = row.equipmentId || ids.value;
  try {
    await ElMessageBox.confirm(`是否确认删除VIEW编号为"${_equipmentIds}"的数据项？`, '警告', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning'
    });
    await delEquipmentPowerReport(_equipmentIds);
    getList();
    ElMessageBox.alert('删除成功', '提示');
  } catch (error) {
    console.log('取消删除');
  }
};

// 导出操作
const handleExport = async () => {
  try {
    await exportEquipmentPowerReport(queryParams);
    console.log('导出成功');
  } catch (error) {
    console.log('导出失败');
  }
};

// 表单重置
const resetForm = () => {
  Object.keys(form).forEach(key => {
    form[key] = null;
  });
};

onMounted(() => {
  getList();
});
</script>